Page({
  data: {
    currentTab: 0,
    tabs: ['试卷分析', '学生分析', '知识点分析'],
    subjects: ['全部', '语文', '数学', '英语'],
    currentSubject: '全部',
    dateRange: ['全部时间', '近一周', '近一月', '近三月'],
    currentDateRange: '全部时间',
    searchKeyword: '',
    paperList: [],
    paperStatsData: {
      avgScore: 85.6,
      passRate: 92.5,
      excellentRate: 45.3,
      totalStudents: 125
    },
    scoreDistribution: [
      { score: '0-60', count: 9, percentage: '7.2%' },
      { score: '60-70', count: 12, percentage: '9.6%' },
      { score: '70-80', count: 26, percentage: '20.8%' },
      { score: '80-90', count: 38, percentage: '30.4%' },
      { score: '90-100', count: 40, percentage: '32.0%' }
    ],
    paperRanking: [],
    studentRanking: [],
    knowledgePoints: [],
    isLoading: false,
    showFilterPopup: false
  },

  onLoad: function() {
    this.loadStatisticsData();
  },

  // 返回上一页
  onBackClick: function() {
    wx.navigateBack({
      delta: 1
    });
  },

  // 切换选项卡
  switchTab: function(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      currentTab: index
    });
    this.loadStatisticsData();
  },

  // 选择学科筛选
  selectSubject: function(e) {
    const subject = e.currentTarget.dataset.subject;
    this.setData({
      currentSubject: subject,
      showFilterPopup: false
    });
    this.loadStatisticsData();
  },

  // 选择时间范围
  selectDateRange: function(e) {
    const range = e.currentTarget.dataset.range;
    this.setData({
      currentDateRange: range,
      showFilterPopup: false
    });
    this.loadStatisticsData();
  },

  // 搜索
  searchStats: function(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    this.loadStatisticsData();
  },

  // 显示筛选弹窗
  showFilter: function() {
    this.setData({
      showFilterPopup: true
    });
  },

  // 关闭筛选弹窗
  closeFilter: function() {
    this.setData({
      showFilterPopup: false
    });
  },

  // 加载统计数据
  loadStatisticsData: function() {
    if (this.data.isLoading) return;
    
    const { currentTab, currentSubject, currentDateRange, searchKeyword } = this.data;
    
    this.setData({ isLoading: true });
    
    // 这里应该是云函数调用，暂时使用模拟数据
    setTimeout(() => {
      // 根据当前标签页加载不同数据
      if (currentTab === 0) {
        this.loadPaperStats();
      } else if (currentTab === 1) {
        this.loadStudentStats();
      } else {
        this.loadKnowledgeStats();
      }
      
      this.setData({ isLoading: false });
    }, 500);
  },

  // 加载试卷分析数据
  loadPaperStats: function() {
    // 模拟试卷排名数据
    const paperRanking = [];
    for (let i = 1; i <= 10; i++) {
      const subject = ['语文', '数学', '英语'][Math.floor(Math.random() * 3)];
      
      if (this.data.currentSubject !== '全部' && this.data.currentSubject !== subject) {
        continue;
      }
      
      const title = `${subject}${['期中测试', '期末测试', '模拟考试', '单元测试'][Math.floor(Math.random() * 4)]}`;
      
      if (this.data.searchKeyword && !title.includes(this.data.searchKeyword)) {
        continue;
      }
      
      paperRanking.push({
        id: `p${i}`,
        rank: i,
        title: title,
        subject: subject,
        avgScore: (70 + Math.random() * 20).toFixed(1),
        passRate: (80 + Math.random() * 20).toFixed(1) + '%',
        studentCount: 30 + Math.floor(Math.random() * 70)
      });
    }
    
    this.setData({
      paperRanking: paperRanking
    });
  },

  // 加载学生分析数据
  loadStudentStats: function() {
    // 模拟学生排名数据
    const studentRanking = [];
    for (let i = 1; i <= 10; i++) {
      studentRanking.push({
        id: `s${i}`,
        rank: i,
        name: `学生${i}`,
        studentId: `10${i}`,
        avgScore: (70 + Math.random() * 20).toFixed(1),
        examCount: 5 + Math.floor(Math.random() * 10),
        bestSubject: ['语文', '数学', '英语'][Math.floor(Math.random() * 3)]
      });
    }
    
    this.setData({
      studentRanking: studentRanking
    });
  },

  // 加载知识点分析数据
  loadKnowledgeStats: function() {
    // 模拟知识点掌握度数据
    const knowledgePoints = [];
    
    // 语文知识点
    if (this.data.currentSubject === '全部' || this.data.currentSubject === '语文') {
      knowledgePoints.push(
        { id: 'k1', name: '文言文阅读', subject: '语文', masteryRate: (60 + Math.random() * 30).toFixed(1) + '%' },
        { id: 'k2', name: '现代文阅读', subject: '语文', masteryRate: (70 + Math.random() * 20).toFixed(1) + '%' },
        { id: 'k3', name: '写作', subject: '语文', masteryRate: (65 + Math.random() * 25).toFixed(1) + '%' }
      );
    }
    
    // 数学知识点
    if (this.data.currentSubject === '全部' || this.data.currentSubject === '数学') {
      knowledgePoints.push(
        { id: 'k4', name: '函数', subject: '数学', masteryRate: (75 + Math.random() * 20).toFixed(1) + '%' },
        { id: 'k5', name: '导数', subject: '数学', masteryRate: (65 + Math.random() * 25).toFixed(1) + '%' },
        { id: 'k6', name: '概率统计', subject: '数学', masteryRate: (60 + Math.random() * 30).toFixed(1) + '%' }
      );
    }
    
    // 英语知识点
    if (this.data.currentSubject === '全部' || this.data.currentSubject === '英语') {
      knowledgePoints.push(
        { id: 'k7', name: '阅读理解', subject: '英语', masteryRate: (70 + Math.random() * 20).toFixed(1) + '%' },
        { id: 'k8', name: '完形填空', subject: '英语', masteryRate: (65 + Math.random() * 25).toFixed(1) + '%' },
        { id: 'k9', name: '写作', subject: '英语', masteryRate: (60 + Math.random() * 30).toFixed(1) + '%' }
      );
    }
    
    this.setData({
      knowledgePoints: knowledgePoints
    });
  },

  // 查看试卷详情
  viewPaperDetail: function(e) {
    const paperId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/teacher/paper/detail/detail?id=${paperId}`,
    });
  },

  // 查看学生详情
  viewStudentDetail: function(e) {
    const studentId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/teacher/student/detail/detail?id=${studentId}`,
    });
  },

  // 导出统计数据
  exportStatistics: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '导出成功',
        icon: 'success'
      });
    }, 1500);
  }
}) 